//
//  Check.m
//  DWCoach
//
//  Created by Mxionlly on 16/9/29.
//  Copyright © 2016年 dawen_plus. All rights reserved.
//

#import "Check.h"
#import <AVFoundation/AVFoundation.h>


@implementation Check

+(bool)checkLenght:(NSString*)strmessage
{
    bool isflag = YES;
    NSString *str = [strmessage stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSString *tempStr = [str stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    strmessage=[tempStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    if(strmessage==nil || strmessage.length<=0 || [strmessage isEqualToString:@"<null>"])
    {
        isflag=NO;
    }
    return isflag;
}

+(bool)checkNullLenght:(NSString*)strmessage
{
    bool isflag = YES;
    strmessage=[strmessage stringByReplacingOccurrencesOfString:@" " withString:@""];
    if(strmessage==nil || strmessage.length<=0 || [strmessage isEqualToString:@"<null>"])
    {
        isflag=NO;
    }
    return isflag;
}


//判断邮箱地址
+(BOOL)validateEmail:(NSString *)candidate
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:candidate];
}


//身份证号
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xXyY])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

//判断文本是否为空，YES:6-16位,而且不能是0,NO:反之
+(bool)checkRangepassword:(NSString*)strmessage
{
    bool isflag = YES;
    strmessage=[strmessage stringByReplacingOccurrencesOfString:@" " withString:@""];
    if(strmessage.length < 6 || strmessage.length > 16 || [strmessage isEqualToString:@"0"])
    {
        isflag=NO;
    }
    
    return isflag;
}

//判断是否是4位数验证码
+(bool)checkRangeCode:(NSString*)Code
{
    NSString * regex        = @"^[0-9]\\d*$";
    NSPredicate * pred      = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch            = [pred evaluateWithObject:Code];
    if (isMatch && Code.length == 4) {
        return YES;
    }else{
        return NO;
    }
}


//正则判断手机号码地址格式
+(BOOL)isMobileNumber:(NSString *)mobileNum
{
    mobileNum =[mobileNum stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (mobileNum.length == 11) {
        NSString *MOBILE = @"^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
        NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
        return [regextestmobile evaluateWithObject:mobileNum];
    }
    return NO;
}

//是否包含汉字和字母
+(BOOL)IsABChinese:(NSString *)string
{
    NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSInteger count = [numberRegular numberOfMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length)];
    if (count > 0) {
        return YES;
    }else{
        return [self IsChinese:string];
    }
}

//是否包含汉字
+(BOOL)IsChinese:(NSString *)string
{
    for(int i=0; i< [string length];i++){
        int a = [string characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)return YES;
    }
    return NO;
}

+ (NSString *)getTime:(NSString *)date withFormat:(NSString *)format {
    NSDate *beDate = [NSDate dateWithTimeIntervalSince1970:[date doubleValue]/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC+0800"];
    [formatter setDateFormat:format];
    return [formatter stringFromDate:beDate];
}


+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}
@end
